package com.demo.rigistry.servlet.myservlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/httpservlet/post")
public class HttpServletTest extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public HttpServletTest() {
		super();
	}

	public void init(ServletConfig config) throws ServletException {
		System.out.println("HttpServletTest：初始化！");
	}

	public void destroy() {
		System.out.println("HttpServletTest：销毁！");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

		// HTTP请求输入流建立一个BufferedReader对象
		InputStreamReader isr = new InputStreamReader(request.getInputStream(), Charset.forName("UTF-8"));
		BufferedReader br = new BufferedReader(isr);
		String name = request.getParameter("name");
		System.out.println("name:" + name);
		
		String str = null;

		while ((str = br.readLine()) != null) {
			System.err.println(str);
		}

		ServletOutputStream writer = response.getOutputStream();
		String date = new Date().toString();
		String params = name + ":" + date;
		writer.write(params.getBytes(Charset.forName("UTF-8")));

		// 这个中文乱码
		// PrintWriter w = response.getWriter();
		// writer.write();
	}

}
